/**
 * Created by xinxin on 2016/10/30.
 */
var pageIndex;
var currentContractId;
//baseUrl = "http://localhost:8080/";
baseUrl = "/";
$(document).ready(function () {
    getContractList(1);
    $("#getContent").click(getContractList(pageIndex));
});
function getContractList(index) {
    var data = "";
    $.ajax({
        type: "GET",
        url: baseUrl + "specific_lawyer/json_contract_all/" + index,
        data: data,
        dataType: 'json',
        contentType: 'application/json;chartset=utf-8',
        success: function (msg) {
            //$("#showdownlist").text(JSON.stringify(msg));
            console.log(msg);
            showContractList(msg);
        }
    })
}
function getConCondition(index) {
    //var t = $("#conditionForm").serializeArray();
    var d = {};
    d["pageIndex"] = index;
    return JSON.stringify(d);
}

function getContractItemDetail(_url) {
    var arr = _url.split('\/');
    console.log(arr);
    currentContractId = arr[arr.length - 1];
    $.getJSON(_url, function (json) {
        if (json != "" && json.status == 1) {
            var html = solveTextToShow(json.data);
            $("#lawclasspreview").show();
            $("#contractpagelink").hide();
            $("#showdownlist").hide();
            $("#Previewpage").html(html);
        } else {
            alert(json.mesg);
        }
    });
    return false;
}
function downLoadDocument() {
    console.log("文件下载");
    var url = baseUrl + "specific_lawyer/download_contract/" + currentContractId;
    console.log(url);
    window.open(url);
}
function solveTextToShow(data) {
    data = data.replace(new RegExp("\\r\\n", "gm"), "<\/br>");
    data = data.replace(new RegExp("\\t", "gm"), "&nbsp&nbsp&nbsp&nbsp");
    console.log(data);
    return data;
}
function showContractList(msg) {
    var html = "";
    var html2 = "";
    var htmlpage = "";
    if (msg.data != null && msg.status === 1) {
        //$(".page2").show();
        pageIndex = msg.pageNum;
        $("#error").hide();
        $("#showdownlist").show();
        var listpre = msg.data;
        var list = eval(listpre);
        //html += "<ul id='formBody' class='clearfx' style='width: 100%'>";
        for (var position in list) {
            //html += "<li><span class='float_left'><a href='" + baseUrl + "specific_lawyer/json_contract/" + list[position].id + "' onclick='javascript:getContractItemDetail($(this).attr(\"href\"));return false;'>" + list[position].name + "</a></span><span class='float_right date'>" + list[position].enterTime + "</span></li>";
            html2 += " <tr>" +
                "<td height='20' bgcolor='#FFFFFF' ><div" +
                " align='center'> " +
                "<span>" + "<a href='" + baseUrl + "specific_lawyer/json_contract/" + list[position].id + "' onclick='javascript:getContractItemDetail($(this).attr(\"href\"));return false;'>" + list[position].name + "</a>" + "</span> " +
                "</div></td> " +
                "<td height='20' bgcolor='#FFFFFF' ><div" +
                " align='center'>" +list[position].enterTime+"</div></td> </tr>";
        }
        //html += "</ul>";

        //设置页数导航条的自动生成
        htmlpage += "<a id='frontpage' class='previous font12' href='javascript:firstConIndex();'>首页</a>&nbsp;";
        htmlpage += "<a id='fpage' class='previous font12' href='javascript:preConIndex();'>上一页</a>&nbsp;";
        htmlpage += "<a id='lpage' class='previous font12' href='javascript:nextConIndex()'>下一页</a>&nbsp;";
    } else {
        alert(msg.mesg);
        //设置页数导航条的自动生成
        htmlpage += "<a id='frontpage' class='previous font12' href='javascript:firstConIndex();'>首页</a>&nbsp;";
        htmlpage += "<a id='fpage' class='previous font12' href='javascript:preConIndex();'>上一页</a>&nbsp;";
        $("#contractpagelink").empty().html(htmlpage);
        $("#error").text(msg.mesg).show();
    }

    $("#tr_tile").nextAll().remove();
    $("#tr_tile").parent().append(html2);
    //$("#showdownlist").empty().html(html);
    $("#contractpagelink").empty().html(htmlpage);
}
function firstConIndex() {
    getContractList(1);
}
function nextConIndex() {
    getContractList(pageIndex + 1);
}
function preConIndex() {
    getContractList(pageIndex - 1);
}
function selectConIndex() {
    getContractList(parseInt($(this).text()));
}
